package de.tum.activities.ExamEvaluation;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.widget.Toast;

public class DrawOnTop extends View {
	Paint mPaintBlack;
	Paint mPaintYellow;
	byte[] mYUVData;
	int[] mRGBData;
	int mImageWidth, mImageHeight;
	int[] mGrayHistogram;
	double[] mGrayCDF;
	int mState;
	int touchX, touchY;

	static boolean landscape_view;
	static Bitmap mBitmap;
	static double touchPercentageInImageLeft, touchPercentageInImageTop, touchPercentageInImageRight, touchPercentageInImageBottom;
	static int canvasWidth, canvasHeight;
	
	Context context;
	
	static final int STATE_PROCESSED = 1;

    public DrawOnTop(Context context) {
        super(context);
        
        this.context = context;
        
        mPaintBlack = new Paint();
        mPaintBlack.setStyle(Paint.Style.FILL);
        mPaintBlack.setColor(Color.BLACK);
        mPaintBlack.setTextSize(25);
        
        mPaintYellow = new Paint();
        mPaintYellow.setStyle(Paint.Style.FILL);
        mPaintYellow.setColor(Color.YELLOW);
        mPaintYellow.setTextSize(25);
        
        mBitmap = null;
        mYUVData = null;
        mRGBData = null;
        mGrayHistogram = new int[256];
        mGrayCDF = new double[256];
        mState = STATE_PROCESSED;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBitmap != null)
        {
        	canvasWidth = canvas.getWidth();
        	canvasHeight = canvas.getHeight();
        	int orientation = context.getResources().getConfiguration().orientation;
        	int newImageWidth;
        	int newImageHeight;
        	int marginWidth;
        	if(orientation == Configuration.ORIENTATION_LANDSCAPE){
        		newImageWidth = 640;
        		newImageHeight = 480;
            	marginWidth = (canvasWidth - newImageWidth)/2;
        	}else{
            	newImageWidth = 480;
            	newImageHeight = 640;
            	marginWidth = (canvasHeight - newImageHeight)/2;
        	}

        	     
        	
        	
        	decodeYUV420RGBContrastEnhance(mRGBData, mYUVData, mImageWidth, mImageHeight);
        	
        	if(orientation == Configuration.ORIENTATION_LANDSCAPE){
        		
        		landscape_view = true;
        		
        		// Draw bitmap
            	mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
            	Rect src = new Rect(0, 0, mImageWidth, mImageHeight);
            	Rect dst = new Rect(marginWidth, 0, canvasWidth-marginWidth, canvasHeight);
            	canvas.drawBitmap(mBitmap, src, dst, mPaintBlack);
            	
            	// Draw black borders        	        	
            	canvas.drawRect(0, 0, marginWidth, canvasHeight, mPaintBlack);
            	canvas.drawRect(canvasWidth - marginWidth, 0, 
            			canvasWidth, canvasHeight, mPaintBlack);
            	
            	Paint paint = new Paint();
            	paint.setStyle(Paint.Style.STROKE);
            	paint.setColor(Color.BLUE);
            	paint.setStrokeWidth(3);
            	
            	touchPercentageInImageLeft = (touchX-60 - marginWidth)/(double)(newImageWidth);
            	touchPercentageInImageTop = (touchY-60) /(double)newImageHeight;
            	touchPercentageInImageRight= (touchX+60 - marginWidth)/(double)(newImageWidth);
            	touchPercentageInImageBottom = (touchY+60) /(double)newImageHeight;
            	
//            	Log.d("PERCENTAGE IN IMAGE", "X-Percent : "+ touchPercentageInImageLeft+" , Y-Percent : "+touchPercentageInImageBottom);
//            	Log.d("CANVAS SIZE", "canvas width : "+ canvasWidth+" , CanvasHeight : "+canvasHeight);
            	
            	canvas.drawRect((int)(marginWidth+touchPercentageInImageLeft*newImageWidth), (int)(touchPercentageInImageTop*newImageHeight), (int)(marginWidth+touchPercentageInImageRight*newImageWidth), (int)(touchPercentageInImageBottom*newImageHeight), paint);
   				
//   				Log.d("DRAWONTOP_TOUCH", "X : "+touchX+" , Y : "+touchY);
            	
        	}else{
        		landscape_view = false;
        		
        		// Draw bitmap
            	mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
            	
            	Matrix m = new Matrix();
            	m.postRotate(90);
            	
            	Bitmap rotatedBitmap = Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, true);

            	
            	Rect src = new Rect(0, 0, mImageHeight, mImageWidth);
            	Rect dst = new Rect(0, marginWidth, canvasWidth, canvasHeight-marginWidth);
            	canvas.drawBitmap(rotatedBitmap, src, dst, mPaintBlack);

        		// Draw black borders        	        	
            	canvas.drawRect(0, 0, canvasWidth, marginWidth, mPaintBlack);        	        	
            	canvas.drawRect(0, canvasHeight- marginWidth, canvasWidth, canvasHeight, mPaintBlack);
            	
            	Paint paint = new Paint();
            	paint.setStyle(Paint.Style.STROKE);
            	paint.setColor(Color.BLUE);
            	paint.setStrokeWidth(3);
            	
            	touchPercentageInImageLeft = (touchX-60)/(double)(newImageWidth);
            	touchPercentageInImageTop = (touchY-60 - marginWidth) /(double)newImageHeight;
            	touchPercentageInImageRight= (touchX+60)/(double)(newImageWidth);
            	touchPercentageInImageBottom = (touchY+60 - marginWidth) /(double)newImageHeight;
            	
//            	Log.d("PERCENTAGE IN IMAGE", "X-Percent : "+ touchPercentageInImageLeft+" , Y-Percent : "+touchPercentageInImageBottom);
//            	Log.d("CANVAS SIZE", "canvas width : "+ canvasWidth+" , CanvasHeight : "+canvasHeight);
            	
            	canvas.drawRect((int)(touchPercentageInImageLeft*newImageWidth), (int)(marginWidth+touchPercentageInImageTop*newImageHeight), (int)(touchPercentageInImageRight*newImageWidth), (int)(marginWidth+touchPercentageInImageBottom*newImageHeight), paint);
   				
//   				Log.d("DRAWONTOP_TOUCH", "X : "+touchX+" , Y : "+touchY);
            
            	
        	}
        	
        } // end if statement
        
        super.onDraw(canvas);
        
    } // end onDraw method


    
    private void decodeYUV420RGBContrastEnhance(int[] rgb, byte[] yuv420sp, int width, int height) {
    	// Compute histogram for Y
    	final int frameSize = width * height;
    	int clipLimit = frameSize / 10;
    	for (int bin = 0; bin < 256; bin++)
    		mGrayHistogram[bin] = 0;
    	for (int j = 0, yp = 0; j < height; j++) {
    		for (int i = 0; i < width; i++, yp++) {
    			int y = (0xff & ((int) yuv420sp[yp])) - 16;
    			if (y < 0) y = 0;
    			if (mGrayHistogram[y] < clipLimit)
    				mGrayHistogram[y]++;
    		}
    	}
    	double sumCDF = 0;
    	for (int bin = 0; bin < 256; bin++)
    	{
    		sumCDF += (double)mGrayHistogram[bin]/(double)frameSize;
    		mGrayCDF[bin] = sumCDF;
    	}
    	
    	// Convert YUV to RGB
    	for (int j = 0, yp = 0; j < height; j++) {
    		int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
    		for (int i = 0; i < width; i++, yp++) {
    			int y = (0xff & ((int) yuv420sp[yp])) - 16;
    			if (y < 0) y = 0;
    			if ((i & 1) == 0) {
    				v = (0xff & yuv420sp[uvp++]) - 128;
    				u = (0xff & yuv420sp[uvp++]) - 128;
    			}
    			y = (int)(mGrayCDF[y]*255 + 0.5);
    			
    			int y1192 = 1192 * y;
    			int r = (y1192 + 1634 * v);
    			int g = (y1192 - 833 * v - 400 * u);
    			int b = (y1192 + 2066 * u);
    			
    			if (r < 0) r = 0; else if (r > 262143) r = 262143;
    			if (g < 0) g = 0; else if (g > 262143) g = 262143;
    			if (b < 0) b = 0; else if (b > 262143) b = 262143;
    			
    			rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
    		}
    	}
    }
    

    
}
